define('utils/helpers', null, function() {

let h = {
    /**
     * h.U('posts', {id: 1}) -> /index.php?r=posts&id=1 或 /posts/1
     */
    U: function(url, params, subfix) {
        let query = '';
        let pretty = '1' === Fate.PRETTY_ROUTER;

        if(!params) {
            params = {};
        }
        if(!subfix) {
            subfix = '';
        }

        if(pretty) {
            let firstParam = '';
            for(let k in params) {
                firstParam = '/' + params[k];
                break;
            }
            url = '/' + url + firstParam + subfix;

        } else {
            url = '/index.php?r=' + url;
            // id=1&age=20&
            for(let k in params) {
                query = query + k + '=' + params[k] + '&';
            }

            if(query) {
                url += '&' + query;

                url = url.substring(0, url.length - 1);
            }
        }

        return url;
    },
    getQueryString: function(name) {
        let search = window.location.search;

        search = search.replace('?', '');
        if('' === search) {
            return '';
        }

        let params = search.split('&');
        let ret = '';
        let tmp = null;
        for(let i = 0, len = params.length; i < len; i++) {
            tmp = params[i].split('=');
            if(name === tmp[0]) {
                ret = decodeURIComponent(tmp[1]);
                break;
            }
        }

        return ret;
    },
    json2Query: function(obj) {
        let ret = '';

        for(let k in obj) {
            ret += (k + '=' + encodeURIComponent(obj[k]) + '&');
        }

        return ret;
    },
    nl2br: function(str) {
        return str.replace(/(\015\012)|(\012\015)|(\015)|(\012)/g, '<br />');
    },
    filterTags: function(str, allowed) {
        let tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi;
        let comments = /<!--[\s\S]*?-->/gi;

        str = str.replace(comments, '');

        if(undefined === allowed) {
            return str.replace(tags, '');
        }

        allowed = allowed.toLowerCase();

        // match 为匹配到的内容  p 为第一个子模式
        return str.replace(tags, (match, p) => {
            return allowed.indexOf('<' + p.toLowerCase() + '>') !== -1 ? match : '';
        });
    },
    htmlSpecialchars: function(str) {
        str = str.replace(/</g, '&lt;').replace(/>/g, '&gt;');
        str = str.replace(/'/g, '&#039;').replace(/"/g, '&quot;');

        return str;
    },
    shortNumber: function(n) {
        let ret = Number(n);

        if(ret < 1000) {
            return n;
        }

        if(ret >= 1000 && ret < 10000) {
            return (ret / 1000).toFixed(1) + 'k';
        }

        if(ret >= 10000 && ret < 10000000) {
            return (ret / 10000).toFixed(1) + 'w';
        }

        if(ret >= 10000000 && ret < 100000000) {
            return (ret / 10000000).toFixed(1) + 'kw';
        }

        return (ret / 100000000).toFixed(1) + 'hm';
    },
    getAvatar: function(url) {
        if(url) {
           return url;
        }

        return Fate.STATIC + '/pc/images/avatar.jpg';
    },
    uploadPath: function(path) {
        return Fate.UP + path;
    },
    /**
     * 列表转树
     *
     * @param {Array} list
     * @param {String} primaryKey
     * @param {String} parentKey
     * @param {String} parentValue
     */
    list2Tree: function(list, primaryKey/* = id*/, parentKey/* = pid*/, parentValue/* = '0'*/) {
        let ret = [];

        for(let i=0, len=list.length; i<len; i++) {
            if(parentValue === String(list[i][parentKey])) {
                list[i].children = [];
                ret.push(list[i]);
            }
        }

        for(let i=0, len=list.length; i<len; i++) {
            if(parentValue === String(list[i][parentKey])) {
                continue;
            }

            for(let x=0,l=ret.length; x<l; x++) {
                if(list[i][parentKey] === ret[x][primaryKey]) {
                    ret[x].children.push(list[i]);
                }
            }
        }

        return ret;
    },
    trimChar: function(str, character) {
        if(character === str.charAt(0)) {
            str = str.substring(1);
        }
        if(character === str.charAt(str.length - 1)) {
            str = str.substring(0, str.length - 1);
        }

        return str;
    },
    addClass: function(element, className) {
        if(-1 !== element.className.indexOf(className)) {
            return;
        }

        element.className = element.className + ' ' + className;
    },
    removeClass: function(element, className) {
        let newClassName =  ' ' + element.className + ' ';
        let replaced = newClassName.replace(' ' + className + ' ', ' ');

        element.className = h.trimChar(replaced, ' ');
    }
};

return h;
});
